﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Reflection;
using HF.Software.ClassLib;
using HF.Software.Entities;

namespace HF.Software
{
    /// <summary>
    /// hef创建于2015.02.04
    /// 自助机配置文件读写
    /// </summary>
    public partial class frmSelf : Form
    {

        #region event

        /// <summary>
        /// 浏览
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSelected_Click(object sender, EventArgs e)
        {
            OpenFileDialog oFileDialog = new OpenFileDialog();
            oFileDialog.InitialDirectory = @"c:\";
            oFileDialog.RestoreDirectory = true;
            //oFileDialog.Filter = "(*.wmv)|*.*";//过滤单个类型文件
            //oFileDialog.Filter = "(*.wmv)|*.*|mp4|*.mp4";//选择类型过滤文件
            oFileDialog.Filter = "(*.wmv,*.mp4)|*.wmv;*.mp4";//过滤多种格式文件
            DialogResult oDialogResult = oFileDialog.ShowDialog();
            if (oDialogResult == System.Windows.Forms.DialogResult.OK)
            {
                this.txtPath.Text = oFileDialog.FileName.Remove(oFileDialog.FileName.LastIndexOf('\\')
                                                , oFileDialog.FileName.Length - oFileDialog.FileName.LastIndexOf('\\'));
            }
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (null == this.oConfig) return;

            IniModelDataCollection oDataCollection = new IniModelDataCollection();
            IniWrite oIniWrite = new IniWrite();

            SELF oSelf = this.oConfig.SELF;

            try
            {
                PropertyInfo[] oFields = oSelf.GetType().GetProperties();

                foreach (PropertyInfo oInfo in oFields)
                {
                    IniModel oConfigIni = new IniModel();
                    oConfigIni.IN_Section = "SELF";
                    oConfigIni.IN_Key = oInfo.Name;
                    oConfigIni.IN_Value = Convert.ToString(oInfo.GetValue(oSelf, null));

                    switch (oConfigIni.IN_Key.Trim())
                    {
                        case "MediaPath"://广告地址
                            oConfigIni.IN_Value = this.txtPath.Text.Trim();
                            break;

                        case "MediaFileExtension"://广告格式
                            oConfigIni.IN_Value = this.cmbExt.SelectedValue.ToString().Trim();
                            break;

                        case "SelfTicketExitPassWord"://关闭页面密码
                            oConfigIni.IN_Value = this.txtPwd.Text.Trim();
                            break;

                        case "SelfTicketSeatRules"://是否启动选座规则
                            if (this.chkRule.Checked)
                                oConfigIni.IN_Value = "T";
                            else
                                oConfigIni.IN_Value = "F";
                            break;
                    }

                    oDataCollection.Add(oConfigIni);
                }

                foreach (IniModel oIniModel in oDataCollection)
                {
                    oIniWrite.Write(oIniModel.IN_Section, oIniModel.IN_Key, oIniModel.IN_Value);
                }
                MessageBox.Show("自助机更新成功！", "提示");
            }
            catch (Exception ex)
            {
                this.oLog.fnLog("201502091548自助机更新失败：" + ex.ToString());
                MessageBox.Show("自助机更新失败！", "提示");
            }
        }

        #endregion

        #region create

        public frmSelf()
        {
            InitializeComponent();
        }

        private void frmSelf_Load(object sender, EventArgs e)
        {
            if (null == this.oConfig) return;

            List<ClsComboBoxItem> lstCmbExt = new List<ClsComboBoxItem>();

            ClsComboBoxItem oCmbExtWmv = new ClsComboBoxItem();
            oCmbExtWmv.Value = "*.wmv,*.mp4";
            oCmbExtWmv.Text = "wmv,mp4";
            lstCmbExt.Add(oCmbExtWmv);

            this.cmbExt.DataSource = lstCmbExt;
            this.cmbExt.DisplayMember = "Text";
            this.cmbExt.ValueMember = "Value";

            this.cmbExt.SelectedValue = this.oConfig.SELF.MediaFileExtension;

            this.txtPath.Text = this.oConfig.SELF.MediaPath;

            this.txtPwd.Text = this.oConfig.SELF.SelfTicketExitPassWord;

            this.chkRule.Checked = this.oConfig.SELF.SelfTicketSeatRules;
        }

        #endregion

        #region var

        AppConfig oConfig = new AppConfig();
        ClsLog oLog = new ClsLog(Application.StartupPath + @"\log\set\" + DateTime.Now.ToString("yyyyMMddHH") + ".txt");

        #endregion
       
    }
}
